<?php
/**
 * Description of ArtistiController
 *
 * @author luigi.pastore
 * @created  4-mag-2011
 */
class ArtistiController extends Classes_Controller
{
    public function init()
    {
	$this->model_artist	= new Classes_Artist();
        
	// Edit...
	if( is_numeric( $id = $this->getParam( 'id' ) ) )
	{
	    // Fetch
	    $artist = $this->model_artist->fetchById( array( $id ) );

	    if( empty( $artist ) )
		trigger_error( 'Nessun Artista' );
	    
	    $this->artist = array_shift( $artist );

            if( !empty( $this->artist ) )
	    {
		# Formattiamo campi
		list( $this->post['post_published_d'], $this->post['post_published_m'], $this->post['post_published_y'] ) = explode( '-', $this->post['post_published_ita'] );
		//$_SESSION['Post']['Info'] = $this->post;
	    }
            
	    # Assegniamo i dati al layout
	    $this->layout->data = $this->artist;

	    $this->layout->artist_id = $this->artist['artist_id'];
	}           
        
        // Dati Form
        $this->form_data = $this->getParams();
    }
    
    public function indexAction()
    {
	$this->layout->content = array( 'script' => 'artisti/index' );
    }
    
    public function infoAction()
    {
	if( $this->form_data['salva'] AND $this->form_data['salva'] !== '' )
	{
	    $this->layout->data = $this->form_data;

            // Mapping...
            $data = array(
                'artist_id' => $this->artist['artist_id'],
                'artist_name' => $this->form_data['artist_name'],
                'artist_info' => $this->form_data['artist_info'],
                'artist_official' => $this->form_data['artist_official'],
                'artist_facebook' => $this->form_data['artist_facebook'],
                'artist_myspace' => $this->form_data['artist_myspace']
            );

            // Edit o Aggiunta?
            if( empty( $this->artist ) )
                $data['artist_createdby'] = $this->application->auth->user['user_id'];
            else
                $data['artist_modifiedby'] = $this->application->auth->user['user_id'];

            $model = new Classes_Artist( $data );

            if( is_numeric( $artist_id = $model->save()->artist_id ) )
            {
                // Redirect...
                return header( 'Location: /admin/artisti/immagine/id/'. $artist_id );
            }
            else
            {
                $errorMsg = $model->errors;

                if( $model->errors['artist_name'] )
                    $errorMsg['artist_name'] = sprintf( ERR_FORMAT, 'Nome Artista' );

                if( $model->errors['artist_info'] )
                    $errorMsg['artist_info'] = ERR_EMPTY;

                if( $model->errors['artist_official'] )
                    $errorMsg['artist_official'] = sprintf( ERR_URL, 'Sito Ufficiale' );

                if( $model->errors['artist_facebook'] )
                    $errorMsg['artist_facebook'] = sprintf( ERR_URL, 'Url Facebook' );

                if( $model->errors['artist_myspace'] )
                    $errorMsg['artist_myspace'] = sprintf( ERR_URL, 'Url MySpace' );
            }          
	}

	# Assegniamo i dati al layout
	$this->layout->errorMsg = $errorMsg;
	$this->layout->content = array( 'script' => 'artisti/info' );
    }
    
    public function immagineAction()
    {
       	if( is_null( $this->artist ) )
	{
	    trigger_error( 'Errore Artista' );
	}
        
	if( $this->form_data['salva'] AND $this->form_data['salva'] !== '' )
	{
            // Rimanda alla pagina di conferma e cancella file temporanei...
            $this->layout->forward = 'artisti/archivio';

            return $this->layout->content = array( 'script' => 'completed' );
	} 
        
	# Assegniamo i dati al layout
	$this->layout->errorMsg = $this->errors;
	$this->layout->content = array( 'script' => 'artisti/immagine' );	 
    }    
    
    public function archivioAction()
    {
	$this->layout->artists = $this->model_artist->fetchAll();
	$this->layout->content = array( 'script' => 'artisti/archivio' );
    }    
    
//	if( $this->uploader->uploaded )
//	{
//	    // L'immagine è stata caricata correttamente...
//	    $this->uploader->allowed = array( 'image/*' );
//	    $this->uploader->image_min_width = 100;
//	    $this->uploader->file_safe_name = false;
//	    $this->uploader->file_auto_rename = false;
//	    $this->uploader->file_overwrite = true;
//	    $this->uploader->jpeg_quality = 72;
//
//	    if( $this->uploader->image_src_x > 190 )
//	    {
//		$this->uploader->image_resize = true;
//		$this->uploader->image_x = 190;
//		$this->uploader->image_ratio_y = true;
//	    }
//
//	    // Processa
//	    if( $process === true )
//	    {
//		// Rinomina e carica...
//		$this->uploader->file_new_name_body = 'artist_' . $artist_id;
//		$this->uploader->process( UPLOAD_DIR );
//
//		if( $this->uploader AND !$this->uploader->processed )
//		{
//		    // Aggiungi l'errore
//		    $errorMsg['artist_image'] = $this->uploader->error;
//		}
//			
//		$this->uploader->clean();
//	    }
//	    else
//	    {
//		$this->uploader->process();
//	    }
//
//	    // Tutto ok?
//	    if( !$this->uploader->processed )
//	    {
//		# Aggiungi l'errore
//		$errorMsg['artist_image'] = $this->uploader->error;
//	    }
//	}
//	else
//	{
//	    // Aggiungi l'errore
//	    $errorMsg['artist_image'] = $this->uploader->error;	    
//	}
	
//	return $errorMsg ? $errorMsg : false;
//    }
}